R para Ciencia de Datos en Salud:
Análisis Descriptivo e Inferencia Estadística
Percy Soto-Becerra M.D., M.Sc(c)
InkaStats Data Science Solutions | Medical Branch
@github/psotob91
Calidad de la investigación médica a menudo es baja.
Código de baja calidad en investigación médica es parte del problema.
Código de baja calidad es más propenso a tener errores.
Reproducibilidad a menudo es engorrosa y consume tiempo.
No cree la tabla “manualmente”.
Genere las tablas con código:
A la tabla descriptiva menudo se la conoce como tablas tipo 1.
Hay muchos paquetes: {flextable}, {gt},{huxtable}, {kableExtra}, {kable}, etc.
Sugerimos {gtsummary} para comenzar:
Permite crear tablas en formato de revistas biomédicas.
Función tbl_summary() para tablas descriptivas univariadas y comparativas (bivariadas)
| id | time | treat | treated | age | race | married2 | procedence | weight | height | e2 |
|---|---|---|---|---|---|---|---|---|---|---|
| 1 | Baseline | Placebo | 0 | 33 | Mestiza | Without couple | Callao | 59.0 | 1.4 | 87.30 |
| 1 | 3 months | Placebo | 0 | 32 | Mestiza | Without couple | Callao | 59.9 | 1.3 | 210.05 |
| 2 | Baseline | Dosis 2 | 1 | 27 | Mestiza | Without couple | Santa Anita | 62.0 | 1.5 | 169.01 |
| 2 | 3 months | Dosis 2 | 1 | 27 | Mestiza | Without couple | Santa Anita | 62.1 | 1.6 | 99.91 |
| 3 | Baseline | Dosis 1 | 1 | 25 | Mestiza | Without couple | Callao | 62.0 | 1.6 | 78.76 |
| 3 | 3 months | Dosis 1 | 1 | 25 | Mestiza | Without couple | Callao | 60.0 | 1.6 | 155.04 |
| Variable | Label |
|---|---|
| id | ID participant |
| time | Time's measurement |
| treat | Treatment's group |
| treated | Treated |
| age | Age, years |
| race | Race |
| married2 | Marital status, recat |
| procedence | Distrit of procedence |
| weight | Weight, kg |
| height | Height, m |
| e2 | Estradiol |
Cuarto tipo de resumenes: continuous, continuos2, categorical y dichotomous
Por defecto, los estadísticos son reportadas como mediana (percentil 25, percentil 75) para variables numéricas y n (%) para variables categóricas/dicotómicas.
Las variables codificadas como 0 / 1, TRUE / FALSE o Yes / No son tratadas como dicotómicas.
Los valores NA se listan como “Unknown
Los atributos de etiqueta se imprimen por defecto.
Uno puede realizar más personalizaciones a la tabla.
datos %>%
select(age, treated, married2, height, e2) %>%
tbl_summary(
type = list(height ~ "continuous"),
statistic = list(
c(age, height) ~ "{mean} ({sd})",
c(married2, treated) ~ "{n} / {N} ({p}%)"
)
)type: Especifica el tipo de variable para el resumen.
statistic: Personaliza los estadísticos reportados.
datos %>%
select(age, treated, married2, height, e2) %>%
tbl_summary(
type = list(c(age, height) ~ "continuous2"),
statistic = list(
c(age, height) ~ c("{mean} ({sd})",
"{median} ({p25} - {p75})"),
c(married2, treated) ~ "{n} / {N} ({p}%)"
)
)type: Especifica el tipo de variable para el resumen.
statistic: Personaliza los estadísticos reportados.
Usar c() para varias variables.
Si queremos reportar más estadísticos en variables numéricas usamos continuous2
datos %>%
select(age, treated, married2, height, e2) %>%
tbl_summary(
type = list(c(age, height) ~ "continuous2"),
statistic = list(
c(age, height) ~ c("{mean} ({sd})",
"{median} ({p25} - {p75})",
"{min} - {max}"),
c(married2, treated) ~ "{n} / {N} ({p}%)"
)
)type: Especifica el tipo de variable para el resumen.
statistic: Personaliza los estadísticos reportados.
Usar c() para varias variables.
Si queremos reportar más estadísticos en variables numéricas usamos continuous2
datos %>%
select(age, treated, married2, height, e2) %>%
tbl_summary(
type = list(c(age, height) ~ "continuous2"),
statistic = list(
c(age, height) ~ c("{mean} ({sd})", "{min} - {max}"),
c(e2) ~ c("{median} ({p25} - {p75})",
"{min} - {max}"),
c(married2, treated) ~ "{n} / {N} ({p}%)"
)
) type: Especifica el tipo de variable para el resumen.
statistic: Personaliza los estadísticos reportados.
Usar c() para varias variables.
Si queremos reportar más estadísticos en variables numéricas usamos continuous2
Podemos ponerle cuantos estadísticos queramos.
Podemos tener diferentes combinaciones de estadísticos.
datos %>%
select(age, treated, married2, height, e2) %>%
tbl_summary(
type = list(c(age, height, e2) ~ "continuous2"),
statistic = list(
c(age, height) ~ c("{mean} ({sd})", "{min} - {max}"),
c(e2) ~ c("{median} ({p25} - {p75})",
"{min} - {max}"),
c(married2, treated) ~ "{n} / {N} ({p}%)"
),
label = list(
treated ~ "Treated with supplement", e2 ~ "Estradiol, UI",
married2 ~ "Marital status"
)
)type: Especifica el tipo de variable para el resumen.
statistic: Personaliza los estadísticos reportados.
label: Cambia o personaliza la etiqueta de la variable.
datos %>%
select(age, treated, married2, height, e2) %>%
tbl_summary(
type = list(c(age, height, e2) ~ "continuous2"),
statistic = list(
c(age, height) ~ c("{mean} ({sd})", "{min} - {max}"),
c(e2) ~ c("{median} ({p25} - {p75})",
"{min} - {max}"),
c(married2, treated) ~ "{n} / {N} ({p}%)"
),
label = list(
treated ~ "Treated with supplement", e2 ~ "Estradiol, UI",
married2 ~ "Marital status"
),
digits = list(
c(age) ~ 1, c(height, e2) ~ 2, c(married2, treated) ~ 1
)
)type: Especifica el tipo de variable para el resumen.
statistic: Personaliza los estadísticos reportados.
label: Cambia o personaliza la etiqueta de la variable.
digit: Especifica el número de decimales de redondeo.
datos %>%
select(age, treated, married2, height, e2) %>%
tbl_summary(
type = list(c(age, height, e2) ~ "continuous2"),
statistic = list(
c(age, height) ~ c("{mean} ({sd})", "{min} - {max}"),
c(e2) ~ c("{median} ({p25} - {p75})", "{min} - {max}"),
c(married2, treated) ~ "{n} / {N} ({p}%)"
),
label = list(
treated ~ "Treated with supplement", e2 ~ "Estradiol, UI",
married2 ~ "Marital status"
),
digits = list(
c(age) ~ 1, c(height, e2) ~ 2, c(married2, treated) ~ 1
),
missing_text = "Missing data"
) datos %>%
select(age, treated, married2, height, e2) %>%
tbl_summary(
type = list(c(age, height, e2) ~ "continuous2"),
statistic = list(
c(age, height) ~ c("{mean} ({sd})", "{min} - {max}"),
c(e2) ~ c("{median} ({p25} - {p75})", "{min} - {max}"),
c(married2, treated) ~ "{n} / {N} ({p}%)"
),
label = list(
treated ~ "Treated with supplement", e2 ~ "Estradiol, UI",
married2 ~ "Marital status"
),
digits = list(
c(age) ~ 1, c(height, e2) ~ 2, c(married2, treated) ~ 1
),
missing = "always", missing_text = "Missing data"
) misisng_text: Permite editar la etiqueta de missing (Unknown por defecto).
missing: Por defecto se presentan los datos perdidos solo si la variable los tiene “ifany”.
datos %>%
select(age, treated, married2, height, e2) %>%
tbl_summary(
type = list(c(age, height, e2) ~ "continuous2"),
statistic = list(
c(age, height) ~ c("{mean} ({sd})", "{min} - {max}"),
c(e2) ~ c("{median} ({p25} - {p75})", "{min} - {max}"),
c(married2, treated) ~ "{n} / {N} ({p}%)"
),
label = list(
treated ~ "Treated with supplement", e2 ~ "Estradiol, UI",
married2 ~ "Marital status"
),
digits = list(
c(age) ~ 1, c(height, e2) ~ 2, c(married2, treated) ~ 1
),
missing = "no"
) misisng_text: Permite editar la etiqueta de missing (Unknown por defecto).
missing: Por defecto se presentan los datos perdidos solo si la variable los tiene “ifany”.
bold_labels(): negrita a las etiquetas de las variables
italicize_levels(): cursiva a los niveles (valores) de las variables
Se puede descargar la tabla en formato MS. Word para reporte reproducible.
Primero se guarda como un objeto de R:
Se puede descargar la tabla en formato MS. Word para reporte reproducible.
Primero se guarda como un objeto de R:
Se puede descargar la tabla en formato MS. Word para reporte reproducible.
Primero se guarda como un objeto de R:
Se puede descargar la tabla en formato MS. Word para reporte reproducible.
Primero se guarda como un objeto de R:
Se puede descargar la tabla en formato MS. Word para reporte reproducible.
Primero se guarda como un objeto de R:
Abra el proyecto report_reprod_univ.Rproj y dentro de este, abra el archivo quarto report_reprod_univ.qmd.
Siga las instrucciones indicadas en este.
Renderice el archivo quarto final.
10:00
Reporte Reproducible Univariado